package linuxbox.util;

import java.util.*;
import linuxbox.common.*;
import linuxbox.exception.*;
import org.apache.log4j.Logger;
import org.jdom.*;

public class ModuleControl {
    protected static Logger logger = Logger.getLogger
        (ModuleControl.class.getName());

    public Element moduleDef = null;
    protected Collection moduleCol = new Vector();

    public ModuleControl(Element mod) {
        moduleDef = mod;
    }

    public void initServlet(ActionServlet servlet) {
        try {
            List modules = moduleDef.getChildren("Module");
            loadModules(modules);

            setServlet(servlet);
        } catch (Exception exc) {
            logger.error(exc.getMessage());
            exc.printStackTrace();
        }
    }

    public void loadModules(List modules) throws LinuxboxException {
        for (Iterator it = modules.iterator(); it.hasNext(); ) {
            Element mod = (Element)it.next();
            if (logger.isDebugEnabled())
              logger.debug("loading module :" + mod.getText());

            BoxModule bm = new BoxModule(mod);

            if (bm.getName() != null)
              moduleCol.add(bm);

            if (logger.isDebugEnabled()) {
              logger.debug("Adding module :" + bm.getName());
            }
        }
    }

    public BoxModule getModule(String name) {
        Iterator it = moduleCol.iterator();
        while (it.hasNext()) {
            BoxModule mod = (BoxModule) it.next();
            if (mod != null) {
                if (mod.getName().compareTo(name) == 0)
                    return mod;
            }
        }

        return null;
    }

    public void setServlet(ActionServlet servlet) {
        logger.debug("setting servlet ... ");

        for (Iterator it = moduleCol.iterator(); it.hasNext(); ) {
            BoxModule bm = (BoxModule)it.next();
            bm.setServlet(servlet);
        }
    }

    public Action getAction(String name) {
        logger.debug("look for action=" + name);
        Action ret = null;
        for (Iterator it = moduleCol.iterator(); it.hasNext(); ) {
            BoxModule bm = (BoxModule)it.next();
            ret = bm.getAction(name);
            if (ret != null) break;
        }

        return ret;
    }

    public Iterator getAllModules() {
        return moduleCol.iterator();
    }

    public Map getHomePageModules() {
        logger.debug("Get home Page Module List");
        Map map = new HashMap();
        for (Iterator it = moduleCol.iterator(); it.hasNext(); ) {
            BoxModule bm = (BoxModule)it.next();
            if (bm.homepageModules != null)
                map.putAll(bm.homepageModules);
        }
        return map;
    }

    public Map getAdminModules() {
        logger.debug("Get Admin Page Module List");
        Map map = new HashMap();
        for (Iterator it = moduleCol.iterator(); it.hasNext(); ) {
            BoxModule bm = (BoxModule)it.next();
            if (bm.adminModules != null)
                map.putAll(bm.adminModules);
        }
        return map;
    }
}
